﻿<?php
class Database(){
	var $con;
	function connect_databse(){
		$con=mysql_connect('localhost','root','')or die('khong the ket noi');
		mysql_select_db('quanli_hocsinh',$con);
	}
	
	function addHocSinh($m_hs,$ten,$phai,$ngay_sinh,$noi_sinh,$m_lop){
		$query="insert into hs values('{$m_hs}','{$ten}','{$phai}','{$ngay_sinh}','{$noi_sinh}','{$m_lop}'";
		try{
			mysql_query($query);
		}
		catch(Exception $e){
			echo "loi khi them hoc sinh";
		}
	}
	function addDiem($m_hs,$m_mh,$d1,$d2,$d3,$d4,$d5,$d6,$dt,$m_hk){
		$query="insert into diem values('{$m_hs}','{$m_mh}','{$d1",'{$d2}','{$d3}','{$d4}','{$d5}','{$d6}','{$dt}','{$m_hk}'";
		try{
			mysql_query($query);
		}
		catch(Exception $e){
			echo "loi khi them diem";
		}
	}
	
	function viewDiemMotHocSinh($m_hs,$m_hk){
		$query="select hs.m_hs,ten,ten_mon,diem_mieng,diem_15p,diem_bs1,diem_bs2,diem_1tiet1,diem_1tiet2,diem_thi,diem_tk,xeploai from mon,hs,diem where mon.m_mh=diem.m_mh and diem.m_hs=hs.m_hs and diem.m_hk='{$m_hk}'and diem.m_hs='{$m_hs}'";
		$row=mysql_fetch_array(mysql_query($query)))
		return $row;	
		
	}
	function viewHocSinhMotLop($m_lop){
		$query="select hs.m_hs,ten,phai,ngay_sinh,noi_sinh from hs,lop where hs.m_lop=lop.m_lop and lop.m_lop='{$m_lop}'";
		$result=mysql_query($query);
		$row=mysql_fetch_array($result);
		return $row;
	}
	function getMaLop(){
		$query="select m_lop from lop";
		$result=mysql_query($query);
		$row=mysql_fetch_array($result);
		return $row;
	}
	function checkAcc($id,$pass){
		$query="select quyen from user where tai_khoan='{$id}' and mat_khau=}'$pass}'";
		$result=mysql_query($query);
		$row=mysql_fetch_array($result);
		return $row['quyen'];
	}
	
	function getMaHocKi(){
		$query="select m_hk from hk";
		$result=mysql_query($query);
		$row=mysql_fetch_array($result);
		return $row;
	}
	function getDiemTongKet($m_hs,$m_hk){
		$row=mysql_fetch_array(mysql_query("select * from diem,hk where diem.m_hk=hk.m_hk and m_hs='{$m_hs}' and hk.m_hk='{$m_hk}'"));
		try{
		$diem=($row['diem_mieng']+$row['diem_15p']+$row['diem_bs1']+$row['diem_bs2']+($row['diem_1tiet1']+$row['diem_1tiet2'])*2+diem_thi*3)/11;		
		if($diem>=8)
			$loai="giỏi";
		else if($diem>=6.5 && $diem< 8)
			$loai="khá";
		else if($diem>=5 && $diem<6.5)
			$loai="trung binh";
		else if($diem>=3&& $diem<5)
			$loai="kém";
		else if($diem<3)
			$loai="yếu";
		$query="update diem set diem_tk='{$diem}', xeploai='{$loai}'where m_hs='{$row['m_hs']}' and '{$row['m_mh'] }'and'{ $m_hk}'";
		mysql_query($query);
		}
		catch(Exception e){
			echo"loi khi them diem";
		}
	}
	function getMaMon(){
		$query="select count(*) from mon";
		$result=mysql_query($query);
		$row=mysql_fetch_row($result);
		return $row;
	}
	
}
?>